﻿
namespace SwtorCalculator {
    using System;
    using System.IO;
    using System.Text;
    using System.Windows;

    /// <summary>
    /// General methods class
    /// </summary>
    internal static class General {
        /// <summary>
        /// Displays error message asking whether error should be written to file or not.
        /// If true writes error to file
        /// </summary>
        /// <param name="errorSource">Error Source</param>
        /// <param name="errorMessage">Error Message</param>
        /// <param name="errorSite">Error Site</param>
        /// <param name="errorStack">Error Stack</param>
        internal static void ErrorMessage( string errorSource, string errorMessage, string errorSite, string errorStack ) {
            if( MessageBox.Show(
                string.Format(
                "{0} Error\n\nError Message:\n{1}\n\nError Target Site:\n{2}\n\nStack Trace:\n{3}\n\n\nWould you like to create an error report?",
                errorSource,
                errorMessage,
                errorSite,
                errorStack ),
                @"Programmer Fail",
                MessageBoxButton.YesNo ) == MessageBoxResult.Yes )
            {
                WriteErrorReport( errorSource, BuildReport( errorSource, errorMessage, errorSite, errorStack ) );
            }
        }

        /// <summary>
        /// Gets the platform name and type
        /// </summary>
        /// <returns>Platform name and type</returns>
        private static string GetOSInfo() {
            string bitType = string.Empty;
            if( IntPtr.Size == 8 )
                bitType = "64-Bit";
            else
                bitType = "32-Bit";

            switch( Environment.OSVersion.Version.ToString().Substring( 0, 3 ) ) {
                case "6.1":
                    return "Windows 7 " + bitType;

                case "6.0":
                    return "Windows Vista " + bitType;

                case "5.2":
                    return "Windows XP " + bitType;

                case "5.1":
                    return "Windows XP " + bitType;

                case "5.0":
                    return "Windows 2000 " + bitType;

                default:
                    return " Unknown OS";
            }
        }

        /// <summary>
        /// Builds an error string with a consistant format
        /// </summary>
        /// <param name="source">Error Source</param>
        /// <param name="message">Error Message</param>
        /// <param name="site">Error Site</param>
        /// <param name="stack">Error Stack</param>
        /// <returns>A formatted string from the error information</returns>
        private static string BuildReport( string source, string message, string site, string stack ) {
            var report = new StringBuilder();

            report.AppendLine( @"=============================================================================================================================================" );
            report.AppendLine( DateTime.Now.ToString() );
            report.AppendLine( GetOSInfo() );
            report.AppendLine();
            report.AppendLine( source + " Error" );
            report.AppendLine();
            report.AppendLine( "Error Target Site:" );
            report.AppendLine( site );
            report.AppendLine();
            report.AppendLine( "Stack Trace:" );
            report.AppendLine( stack );
            report.AppendLine();
            report.AppendLine( "Message:" );
            report.AppendLine( message );

            return report.ToString();
        }

        /// <summary>
        /// Writes the error report to file
        /// </summary>
        /// <param name="source">Error Source</param>
        /// <param name="errReport">String containing all relavant error information</param>
        private static void WriteErrorReport( string source, string errReport ) {
            string fileName = "ErrorReport_" + source;

            if( File.Exists( fileName + ".txt" ) ) {
                StreamWriter write = File.AppendText( fileName + ".txt" );
                write.WriteLine( errReport );
                write.Close();
            }
            else {
                StreamWriter write = File.CreateText( fileName + ".txt" );
                write.WriteLine( errReport );
                write.Close();
            }
        }

        internal static string wrapText( string toWrap ) {
            int wrapWidth = 63;
            if( toWrap.Length <= wrapWidth ) { return toWrap; } // Don't bother wrapping

            string retVal = toWrap;
            bool eos = false;
            bool foundspace = false;
            int i = wrapWidth;

            while( !eos ) {
                while( !foundspace && i >= 0 ) {
                    if( retVal[i] == ' ' ) {
                        foundspace = true;
                        break;
                    }

                    i--; // didn't find a space so backtrack a char
                }

                if( foundspace ) {
                    retVal = retVal.Insert( i + 1, "\r\n" ); // +1 because we want it after the space
                    i++;
                    foundspace = false;
                }

                // Continue to next part of string unless we're at or close to the end
                if( i + wrapWidth >= retVal.Length - 1 )
                    eos = true;
                else
                    i += wrapWidth;
            }

            return retVal;
        }
    }
}
